package com.penghaisoft.microwms.product.web.controller.outwarehouse;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.ProHandOut;
import com.penghaisoft.microwms.common.data.model.ProSellOut;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IWmsCommonService;
import com.penghaisoft.microwms.product.service.IProHandOutService;
import com.penghaisoft.microwms.product.web.ProductWebApp;

import lombok.extern.slf4j.Slf4j;

/**
 * 手工chu库
 * @author liwenjiang
 *
 */
@RestController
@RequestMapping(value = "/hand")
@Slf4j
public class ProHandOutController extends BaseController {
    @Autowired
	private IWmsCommonService wmsCommonService;
    
    @Autowired
    private IProHandOutService proHandOutService;
    
    @PostMapping("list")
    public TransResult list (@RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "rows", defaultValue = "10") int rows,ProHandOut condition){
		Pager<ProHandOut> result = 	proHandOutService.findListByCondition(page, rows, condition);	
    	return success(result);
    	
    }
    /**
     * 手工出库创建
     */
    @PostMapping("insertHand")
    public TransResult insertHand(@RequestBody List<ProHandOut> list){
    	TransResult transResult = new TransResult();
    	if(list==null || list.isEmpty()){
    		transResult.setCode(Constant.FAIL);
    		transResult.setMsg("请选择型号以及数量");
    		return transResult;
    	}else{
    		//创建5位调拨流水号-手工出库单号
    		String handOutNo = wmsCommonService.getProShortSeq5(Constant.SEQ_PREFIX_SHORT_XSC, Constant.SEQ_PREFIX_SHORT_REDIS_PRO_XSC, 1).get(0);
    		Resp resp  = proHandOutService.insert(list, handOutNo, getLoginName());
    		transResult.setCode(resp.getCode());
    		transResult.setMsg(resp.getMsg());
    	}
		return transResult;   	
    }
    /**
     * 手工出库修改-根据手工出库单条修改
     */
    @PostMapping("update")
    public TransResult update(ProHandOut condition){
    	TransResult transResult = new TransResult();
    	transResult.setCode(Constant.SUCCESS);
    	//获取选中的行项目ID
    	ProHandOut hands = new ProHandOut();
    	hands.setActiveFlag("1");
    	hands.setHandOutId(condition.getHandOutId());
    	//查询手工表
    	List<ProHandOut> list = proHandOutService.queryListByAny(hands);
    	if(list !=null && !list.isEmpty()){
    		ProHandOut handOut = list.get(0);//获取list的第一条数据
    		if(handOut.getHandOutStatus()!=null && !handOut.getHandOutStatus().equals("0")){
    			transResult.setCode(Constant.FAIL);
    			transResult.setMsg("该手工出库单号不能修改");
    			return transResult;
    		}
    	}
    	condition.setCreateBy(getLoginName());
    	condition.setGmtCreate(new Date());
    	proHandOutService.update(condition);
    	
		return transResult;
    	
    }
    /**
     * 根据handOutId  查询
     * @return
     */
    @GetMapping("get/id/{handOutId}")
    public TransResult getHandOutId(@PathVariable("handOutId")String handOutId){
    	TransResult transResult = new TransResult();
    	transResult.setCode(Constant.SUCCESS);
		ProHandOut handOut = new ProHandOut();
		handOut.setActiveFlag("1");
		handOut.setHandOutId(Integer.parseInt(handOutId));

		List<ProHandOut> list  = proHandOutService.queryListByAny(handOut);
		if(list != null && !list.isEmpty()){
			ProHandOut tmp = list.get(0);
			if(tmp.getHandOutStatus()==null){
				transResult.setCode(Constant.FAIL);
				transResult.setMsg("该手工出库单号 出库状态未知！");
				return transResult;
			}
			else if(!tmp.getHandOutStatus().equals("0")){
				transResult.setCode(Constant.FAIL);
				transResult.setMsg("该手工出库单号不能修改！");
				return transResult;
			}
			transResult.setData(tmp);
		}

		return transResult;
    	
    }
    
    /**
     * 审核
     * @param handOut
     * @return
     */
    @PostMapping("audit")
    public TransResult audit(ProHandOut handOut){
		TransResult result = new TransResult();
		handOut.setLastModifiedBy(getLoginName());
		handOut.setGmtModified(new Date());
    	
    	ProHandOut pro = new ProHandOut();
    	pro.setActiveFlag("1");
//    	pro.setHandOutNo(handOut.getHandOutNo());
    	pro.setHandOutId(handOut.getHandOutId());
    	
    	List<ProHandOut> list = proHandOutService.queryListByAny(pro);
    	if(list !=null && !list.isEmpty()){
//			0新建 1出库中 2出库完成 3审核通过 4审核不通过
			if(!list.get(0).getHandOutStatus().equals("0")){
				result.setCode(Constant.FAIL);
				result.setMsg("该类型手工出库单不能审核！");
    	        return result;
    		}
    	}
    	//更新时，只改出库状态是  新建的
    	handOut.setStatus("0");
    	if(handOut.getHandOutStatus().equals("4")){
    		handOut.setActiveFlag(Constant.FAIL);
    	}
    	Resp resp = proHandOutService.updateByNo(handOut);
    	result.setCode(resp.getCode());
    	result.setMsg(resp.getMsg());    	  	
		return result;
    	
    }
    @PostMapping("audit/alone")
    public TransResult auditAlone(ProHandOut handOut){
		TransResult result = new TransResult();
		handOut.setLastModifiedBy(getLoginName());
		handOut.setGmtModified(new Date());
    	
    	ProHandOut pro = new ProHandOut();
    	pro.setActiveFlag("1");
//    	pro.setHandOutNo(handOut.getHandOutNo());
    	pro.setHandOutId(handOut.getHandOutId());
		
    	List<ProHandOut> list = proHandOutService.queryListByAny(pro);
    	if(list !=null && !list.isEmpty()){
//			0新建 1出库中 2出库完成 3审核通过 4审核不通过
			if(!list.get(0).getHandOutStatus().equals("0")){
				result.setCode(Constant.FAIL);
				result.setMsg("该类型手工出库单不能审核！");
    	        return result;
    		}
    	}
    	if(handOut.getHandOutStatus().equals("4")){
    		handOut.setActiveFlag(Constant.FAIL);
    	}
    	Resp resp = proHandOutService.update(handOut);
    	result.setCode(resp.getCode());
    	result.setMsg(resp.getMsg());    	  	
		return result;
    	
    }
	
}
